package com.bjfu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.bjfu.common_utils.R;
import com.bjfu.servicebase.exceptionhandler.GuliException;
import com.bjfu.vod.service.impl.VodService;
import com.bjfu.vod.utils.ConstantVodUtils;
import com.bjfu.vod.utils.InitVodClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {
    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @ApiOperation("上传视频到阿里云")
    @PostMapping("uploadAlyiVideo")
    public R uploadVideo(MultipartFile file) throws IOException {
        String id = vodService.uploadVideoAly(file);
        return R.ok().data("id",id);
    }

    //根据视频Id删除阿里云端视频
    @ApiOperation("根据视频Id删除阿里云端视频")
    @DeleteMapping("deleteAlyiVideo/{id}")
    public R deleteVideo(@PathVariable  String id) throws IOException {
        try {
            DefaultAcsClient client = InitVodClient.init(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            // 调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return R.ok();
        } catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败");
        }
    }

    @ApiOperation("根据视频Id批量删除视频")
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestParam(value = "videoList") List<String> videoList){
        System.out.println(videoList);
        vodService.removeMoreAlyVideo(videoList);
        return R.ok();
    }

    @ApiOperation("获取视频播放凭证")
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id){
        DefaultAcsClient client = InitVodClient.init(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
        // 创建获取凭证的request和response
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        request.setVideoId(id);
        try{
            response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth",playAuth);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"获取视频播放凭证失败");
        }
    }

}
